﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.IO;
using TimeSnapperPluginAPI;

namespace SamplePlugins
{
    public partial class RssImporterConfigurationForm : Form
    {
        private IServices m_services;

        public RssImporterConfigurationForm(IServices services)
        {
            InitializeComponent();
            m_services = services;
        }

        public string Feed
        {
            get { return txtUrl.Text; }
            set { txtUrl.Text = value; }
        }

        public string Username
        {
            get { return txtUsername.Text; }
            set { txtUsername.Text = value; }
        }

        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }

        public int FlagColor
        {
            get { return lblColor.BackColor.ToArgb(); }
            set { lblColor.BackColor = Color.FromArgb(value); }
        }

        private void lblColor_Click(object sender, EventArgs e)
        {
            PickColor();
        }

        private void PickColor()
        {
            ColorDialog dlg = new ColorDialog();
            dlg.Color = this.lblColor.BackColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                lblColor.BackColor = dlg.Color;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtUrl.Text != "" && !Uri.IsWellFormedUriString(txtUrl.Text, UriKind.Absolute))
            {
                MessageBox.Show("The RSS path doesn't seem to point to a valid destination. Here are examples: " +
                    System.Environment.NewLine + System.Environment.NewLine +
                    "http://twitter.com/myrssfeed.rss" + System.Environment.NewLine +
                    "file:///c:/MyOutlookCalenderExport.rss", "TimeSnapper");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient c = new System.Net.WebClient();
                if (txtUsername.Text != "")
                {
                    NetworkCredential nc = new NetworkCredential(txtUsername.Text, txtPassword.Text);
                    c.Credentials = nc;
                }
                string xml = c.DownloadString(new Uri(txtUrl.Text));
                int count = FlagHelpers.SaveFlagsFromRss(m_services, xml, lblColor.BackColor.ToArgb());

                MessageBox.Show(string.Format("Extracted and saved {0} flags from the RSS feed.", count), "TimeSnapper - RSS To Flags Plugin");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }
    }
}
